<?php

/**
 * 初始化品牌新手任务
 */
ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\brand\Content as mBrandContent;
use Biz\model\brand\ContentWiki as mContentWiki;
use Biz\model\brand\ShopCity as mShopCity;
use Biz\model\brandDecoration\BrandCard as mBrandCard;
use Biz\model\wiki\WikiBasic as mWikiBasic;
use Biz\model\brand\Brand as mBrand;
use Doraemon\model\MemberExtendInfo as mMemberExtendInfo;
use Doraemon\service\Task\Task;

//认证品牌、付费品牌
$brandList = mBrand::selectByWhere(['uid[>]' => 0, 'status' => mBrand::STATUS_USING, 'brand_type[>=]' => [mBrand::BRAND_TYPE_COMMON]]);
foreach ($brandList as $row) {
    $uid = $row['uid'];
    $brandId = $row['id'];
    $brand_alias = $row['brand_alias'] ? json_decode($row['brand_alias'], true) : [];
    if ($row['brand_name']) {
        Task::asyncHandle($uid, 1);//设置品牌名称
        Task::asyncHandle($uid, 10);//设置品牌名称
    }
    if (!empty($brand_alias)) {
        Task::asyncHandle($uid, 11);//设置品牌别称 仅付费品牌
    }
    if ($row['discription']) {
        Task::asyncHandle($uid, 2);//设置品牌语
        Task::asyncHandle($uid, 12);//设置品牌语
    }
    if ($row['detail']) {
        Task::asyncHandle($uid, 3);//设置品牌简介
        Task::asyncHandle($uid, 13);//设置品牌简介
    }
    $wikiCount = mWikiBasic::selectCountWhere(['brand_id' => $brandId, 'basic_state[!]' => mWikiBasic::STATE_DEL]);
    if ($wikiCount >= 1) {
        Task::asyncHandle($uid, 4);//上传至少 2 个商品
        Task::asyncHandle($uid, 14);//上传至少 2 个商品
    }
    if ($wikiCount >= 2) {
        Task::asyncHandle($uid, 4);//上传至少 2 个商品
        Task::asyncHandle($uid, 14);//上传至少 2 个商品
    }
    $claimCount = mBrandContent::count([
        'brand_id' => $brandId,
        'brand_uid' => 0,
        'audit_status[!]' => mBrandContent::AUDIT_STATUS_AUTO,
    ]);
    if ($claimCount >= 1) {
        Task::asyncHandle($uid, 5);//认领至少 2 条住友内容
        Task::asyncHandle($uid, 15);//认领至少 2 条住友内容
    }
    if ($claimCount >= 2) {
        Task::asyncHandle($uid, 5);//认领至少 2 条住友内容
        Task::asyncHandle($uid, 15);//认领至少 2 条住友内容
    }
    $claimWikiCount = mContentWiki::count([
        'brand_id' => $brandId,
        'operation_mode[!]' => mContentWiki::OPERATION_MODE_AUTO
    ]);
    if ($claimWikiCount >= 1) {
        Task::asyncHandle($uid, 6);//为内容至少关联 1 个商品
        Task::asyncHandle($uid, 16);//为内容至少关联 1 个商品
    }
    $shopCityCount = mShopCity::count(['brand_id' => $brandId, 'city_status' => mShopCity::STATUS_NORMAL]);
    if ($shopCityCount >= 1) {
        Task::asyncHandle($uid, 17);//添加至少 1 个门店 仅付费品牌
    }
    $memberExtendInfo = mMemberExtendInfo::getInfo($uid);
    if (!empty($memberExtendInfo['cover_img'])) {
        Task::asyncHandle($uid, 7);//设置主页头图
        Task::asyncHandle($uid, 18);//设置主页头图
    }
    $brandCardCount = mBrandCard::count(['brand_id' => $brandId, 'card_status' => mBrandCard::CARD_STATUS_NORMAL]);
    if ($brandCardCount >= 1) {
        Task::asyncHandle($uid, 19);//使用任意 1 个模块装修品牌主页
    }
}